# from datetime import datetime
# import json
# import os

from rj.request import RequestHanlder
# from mkl.content import create_transparant_img, advertising_html, create_html
from mkl.content import advertising_html, create_html

from mongo.manage import MongoDBManager

rj = RequestHanlder()

db_name = "productdb"
db_col = "mkl"
sql_con = MongoDBManager()

# 限制返回的字段
fields = {'_id': 0, 'item_img': 0, 'update_time': 0}

import time
from utils.file_operation import load_json_to_dict, save_dict_to_json
from logger.main import setup_logger, logger

# 加载日志
setup_logger()

# 上传id列表
ids_list = load_json_to_dict('upload.json')
for item in ids_list:

    # 数据库商品列表
    pro_list = sql_con.find_all(db_name, db_col, {"item_code": f"{item}"},
                                fields)

    sku_list = []
    upload_data = []

    new_html = advertising_html() + create_html(pro_list[0])

    # print(new_html)
    photos = [
        {
            "photo":
            f"https://oss.caidehen.top/mkl-logo/{pro_list[0]['item_code']}.jpg"
        },
        {
            "photo":
            f"https://oss.caidehen.top/mkl/{pro_list[0]['item_code']}.jpg"
        },
        {
            "photo": "https://oss.caidehen.top/mkl-cxsw.png"
        },
        {
            "photo": "https://oss.caidehen.top/mkl-cxsw.png"
        },
        {
            "photo": "https://oss.caidehen.top/mkl-cxsw.png"
        },
    ]

    for pro in pro_list:
        temp_sku = {
            "code": pro['product_code'],
            "price": round(float(pro['product_price']) * 0.85),
            "stock": 999,
            "specification": {
                "packingSpecificationValue":
                pro['product_code'].split('-')[-1],
                "packingSpecificationUnit": "瓶",
                "minSpecificationValue": 0,
                "minSpecificationUnit": ""
            },
        }
        sku_list.append(temp_sku)

    temp_upload = {
        "code":
        pro_list[0]['item_code'],
        "name":
        pro['item_cn_name'],
        "brandId":
        298,
        "categoryId":
        216,
        "desc":
        new_html,
        "unit":
        "瓶",
        "photos":
        photos,
        "deliveryTime":
        7,
        "status":
        3,
        "skuInfos":
        sku_list,
        "attributes": [
            {
                "attributeName": "别名",
                "attributeValue": pro_list[0]['item_cn_alia']
            },
            {
                "attributeName": "英文名称",
                "attributeValue": pro_list[0]['item_en_name']
            },
             {
                "attributeName" : "纯度/浓度",
                "attributeValue" :  "",
                "required" : "false"
            },
            {
                "attributeName" : "分子式",
                "attributeValue" :  ""
            },
            {
                "attributeName" : "分子量",
                "attributeValue" :  ""
            },
            {
                "attributeName" : "MDL号",
                "attributeValue" :  ""
            },
            {
                "attributeName" : "熔点",
                "attributeValue" :  ""
            },
            {
                "attributeName" : "沸点",
                "attributeValue" :  ""
            },
            {
                "attributeName" : "密度",
                "attributeValue" :  "",
                "required" : "false"
            },
            {
                "attributeName" : "重量",
                "attributeValue" :  ""
            }
        ]
    }
    upload_data.append(temp_upload)
    res = rj.batchSaveOrUpdateProductV2(upload_data)
    save_dict_to_json(upload_data, 'test_upload.json')
    print(res.json())

    break
